package com.ruoyi.user.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.user.domain.UserOrderDetail;
import com.ruoyi.user.domain.UserOrderReview;
import com.ruoyi.user.domain.UserOrders;
import com.ruoyi.user.domain.vo.OrderSubmitRequest;
import com.ruoyi.user.service.IUserOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/orders")
public class UserOrdersController extends BaseController {

    @Autowired
    private IUserOrdersService userOrdersService;

    /**
     * 提交订单
     */
    @PreAuthorize("@ss.hasPermi('user:orders:create')")
    @PostMapping("/add/order")
    public AjaxResult submit(@RequestBody OrderSubmitRequest request) {
        Long orderId = userOrdersService.submitOrder(request);
        return AjaxResult.success().put("orderId", orderId);
    }

    /**
     * 支付订单
     */
    @PreAuthorize("@ss.hasPermi('user:orders:pay')")
    @PostMapping("/{orderId}/pay")
    public AjaxResult pay(@PathVariable Long orderId,
                          @RequestParam(required = false, name = "paymentMethod") Integer paymentMethod) {
        boolean ok = userOrdersService.payOrder(orderId, paymentMethod == null ? 1 : paymentMethod);
        return ok ? AjaxResult.success() : AjaxResult.error("支付失败");
    }

    /**
     * 根据用户ID查看订单
     */
    @PreAuthorize("@ss.hasPermi('user:orders:list')")
    @GetMapping("/by-user/{customerId}")
    public TableDataInfo listByUser(@PathVariable Long customerId) {
        startPage();
        java.util.List<com.ruoyi.user.domain.vo.OrderListItemVO> list = userOrdersService.listOrderSummariesByCustomer(customerId);
        return getDataTable(list);
    }

    /**
     * 根据状态查看订单（需传 customerId 与 status）
     */
    @PreAuthorize("@ss.hasPermi('user:orders:status')")
    @GetMapping("/by-status/{customerId}/{status}")
    public TableDataInfo listByStatus(@PathVariable Long customerId,
                                      @PathVariable Integer status) {
        startPage();
        java.util.List<com.ruoyi.user.domain.vo.OrderListItemVO> list = userOrdersService.listOrderSummariesByStatus(customerId, status);
        return getDataTable(list);
    }

    /**
     * 查看订单详情（含明细）
     */
    @PreAuthorize("@ss.hasPermi('user:orders:detail')")
    @GetMapping("/order/detail/{orderId}")
    public AjaxResult detail(@PathVariable Long orderId) {
        UserOrderDetail detail = userOrdersService.getOrderDetail(orderId);
        return detail == null ? AjaxResult.error("订单不存在") : AjaxResult.success(detail);
    }

    /**
     * 订单评价
     */
    @PreAuthorize("@ss.hasPermi('user:orders:review')")
    @PostMapping("/order/review/{orderId}")
    public AjaxResult review(@PathVariable Long orderId, @RequestBody UserOrderReview review) {
        review.setOrderId(orderId);
        int rows = userOrdersService.addOrderReview(review);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("评价失败");
    }

    @PreAuthorize("@ss.hasPermi('user:orders:count')")
    @GetMapping("/count-by-merchant/{merchantId}")
    public AjaxResult countByMerchant(@PathVariable Long merchantId) {
        int count = userOrdersService.countOrdersByMerchantLastMonth(merchantId);
        return AjaxResult.success().put("count", count);
    }
}
